package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;

/**
 * 目标：
 *
 * @author Ashely
 * @date 2024/6/23 19:39
 */
@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private DishMapper dishMapper;
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
        BusinessDataVO businessDataVO = new BusinessDataVO();
//        LocalDateTime begin = LocalDateTime.of(LocalDate.now(), LocalTime.MIN);
//        LocalDateTime end = LocalDateTime.of(LocalDate.now(), LocalTime.MAX);
        Integer status = Orders.COMPLETED;
        Double turnover = orderMapper.getTurnover(begin,end,status);
        turnover = turnover == null? 0.0 : turnover;
        Integer newUsers =userMapper.getUserByCreateTime(begin,end);
        Integer validOrderCount = orderMapper.getCompletion(begin,end,status);
        Integer sum =orderMapper.getOrderCompletion(begin,end);
        if (sum!=0){
            Double orderCompletionRate = (double) validOrderCount / sum;
            businessDataVO.setOrderCompletionRate(orderCompletionRate);
        }else {
            businessDataVO.setOrderCompletionRate(0.0);
        }
        Double orderUserCount = orderMapper.getOrderUserCount(begin, end, status);
        if(orderUserCount!=0){
            Double d= turnover/orderMapper.getOrderUserCount(begin,end,status);
            Double unitPrice = BigDecimal.valueOf(d).setScale(3, RoundingMode.HALF_UP).doubleValue();
            businessDataVO.setUnitPrice(unitPrice);
        }else {
            businessDataVO.setUnitPrice(0.0);
        }
        

        businessDataVO.setTurnover(turnover);
        businessDataVO.setNewUsers(newUsers);
        businessDataVO.setValidOrderCount(validOrderCount);
        return businessDataVO;



    }

    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
      Integer sold = setmealMapper.countStatus(StatusConstant.ENABLE);
      Integer discontinued = setmealMapper.countStatus(StatusConstant.DISABLE);
      return SetmealOverViewVO.builder()
              .sold(sold)
              .discontinued(discontinued)
              .build();
    }

    @Override
    public DishOverViewVO getOverviewDishes() {
        Integer sold = dishMapper.countStatus(StatusConstant.ENABLE);
        Integer discontinued = dishMapper.countStatus(StatusConstant.DISABLE);
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    @Override
    public OrderOverViewVO getOverviewOrders() {
        Integer waitingOrders = orderMapper.countStatus(Orders.TO_BE_CONFIRMED);
        Integer deliveredOrders = orderMapper.countStatus(Orders.CONFIRMED);
        Integer cancelledOrders = orderMapper.countStatus(Orders.CANCELLED);
        Integer completedOrders = orderMapper.countStatus(Orders.COMPLETED);
        Integer allOrders = orderMapper.countStatus(null);
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .cancelledOrders(cancelledOrders)
                .completedOrders(completedOrders)
                .allOrders(allOrders)
                .build();
    }
}
